const fs =  require("fs");
const archiver = require("archiver");
  function compress(files,name,suffix) {
    var zipPath = `${name}.${suffix}`;
    var output = fs.createWriteStream("./docker/" + zipPath);
    //生成archiver对象，打包类型为tar
    var zipArchiver = archiver(suffix);
    //将打包对象与输出流关联
    zipArchiver.pipe(output);
    for(var i=0; i < files.length; i++) {
    //将被打包文件的流添加进archiver对象中
        if(files[i].type === 1) {
            zipArchiver.append(fs.createReadStream("./" + files[i].file), {'name': files[i].file});
        } else {
            zipArchiver.directory("./" + files[i].file + "/", files[i].file);
        }
    }
    //打包
    zipArchiver.finalize();
    zipArchiver.closed;
}

module.exports = {compress}

